CC            	:= gcc
CPP			  	:= g++
CFLAGS        	:= -std=gnu11 -Wall -Wextra -g
CPPFLAGS      	:= -std=gnu++17 -Wall -Wextra -g

LFLAGS        	:= -lsqlite3

BIN           	:= bin
LIB           	:= lib
INCLUDE       	:= include
OBJ           	:= obj
SRC           	:= src

DEC           	:= $(SRC)/dec
DEF           	:= $(SRC)/def

SRCDIRS       	:= $(shell find $(SRC) -type d)
C_SOURCES     	:= $(wildcard $(addsuffix /*.c,$(SRCDIRS)))
C_OBJECTS     	:= $(addprefix $(OBJ)/,$(notdir $(C_SOURCES:.c=.o)))
CPP_SOURCES   	:= $(wildcard $(addsuffix /*.cpp,$(SRCDIRS)))
CPP_OBJECTS   	:= $(addprefix $(OBJ)/,$(notdir $(CPP_SOURCES:.cpp=.o)))

TARGET        	:= server
OUTPUT_TARGET 	:= $(BIN)/$(TARGET)
$(OUTPUT_TARGET) : $(C_OBJECTS) $(CPP_OBJECTS)
	$(CPP) $(CPPFLAGS) -I $(INCLUDE) -I $(DEC) -I /usr/include -L $(LIB) $^ -o $@ $(LFLAGS)

$(C_OBJECTS) :
	$(CC) $(CFLAGS) -I $(INCLUDE) -I $(DEC) -I /usr/include -c $(shell find $(SRC) -name $(notdir $(patsubst %.o,%.c,$@))) -o $@ $(LFLAGS)

$(CPP_OBJECTS) :
	$(CPP) $(CPPFLAGS) -I $(INCLUDE) -I $(DEC) -I /usr/include -c $(shell find $(SRC) -name $(notdir $(patsubst %.o,%.cpp,$@))) $(LFLAGS) -o $@ $(LFLAGS)

#--------------------------------------------

.PHONY : all run create clean reall

all : $(OUTPUT_TARGET)
	@echo "\nExecuting 'all' complete!"

run : $(OUTPUT_TARGET)
	cd $(BIN) && ./$(TARGET)
	@echo "\nExecuting 'run' complete!"

create : 
	mkdir -p $(SRC) $(LIB) $(INCLUDE) $(BIN) $(DEP) $(OBJ) $(DEF) $(DEC)
	touch $(SRC)/$(TARGET).c
	@echo "\nExecuting 'create' complete!"

clean :
	cd $(OBJ) && rm -f ./*
	rm -f $(OUTPUT_TARGET)
	@echo "\nExecuting 'clean' complete!"

reall :
	make clean
	make all
